package com.xuanxu.framework.common.pojo;

import lombok.Data;
import org.springframework.util.Assert;

/**
 * 通用响应对象
 * @author XuanXu
 * @version 1.0
 * @date 2024-06-14 21:42
 */
@Data
public class ResponseBean<T> {

    /**
     * 状态码
     * @see HttpStatusCodeConstant
     */
    private Integer code;

    /**
     * 状态消息
     * @see HttpStatusCodeConstant
     */
    private String message;

    /**
     * 响应对象
     */
    private T data;

    /**
     * 成功
     */
    public static<T> ResponseBean<T> success(T data) {
        return success(HttpStatusCodeConstant.SUCCESS.getCode(), HttpStatusCodeConstant.SUCCESS.getMessage(), data);
    }

    /**
     * 成功
     */
    public static<T> ResponseBean<T> success(HttpStatusCode http, T data) {
        return success(http.getCode(), http.getMessage(), data);
    }

    /**
     * 成功
     */
    public static<T> ResponseBean<T> success(Integer code, String message, T data) {
        ResponseBean<T> responseBean = new ResponseBean<>();
        responseBean.setCode(code);
        responseBean.setMessage(message);
        responseBean.setData(data);

        return responseBean;
    }

    /**
     * 失败
     */
    public static<T> ResponseBean<T> fail(T data) {
        return fail(HttpStatusCodeConstant.BAD_REQUEST.getCode(), HttpStatusCodeConstant.BAD_REQUEST.getMessage(), data);
    }

    /**
     * 失败
     */
    public static<T> ResponseBean<T> fail(HttpStatusCode http, T data) {
        return fail(http.getCode(), http.getMessage(), data);
    }
    /**
     * 失败
     */
    public static<T> ResponseBean<T> fail(Integer code, String message, T data) {
        Assert.isTrue(!HttpStatusCodeConstant.SUCCESS.getCode().equals(code), "响应状态码必须是错误的");

        ResponseBean<T> responseBean = new ResponseBean<>();
        responseBean.setCode(code);
        responseBean.setMessage(message);
        responseBean.setData(data);

        return responseBean;
    }
}
